#!/bin/bash
##################################################################
# Name stands for "call 2nd GAIA instance" <- C++ progam.
# Compiles GAIA using Makefile, zips up (important C++) source
# code for archiving and runs gaia while logging to a file.
# When finished, create_plots is run (also logged) to plot the
# output. The logs are then copied to the folder created by GAIA.
##################################################################
# Usage: c2gaia.sh "Name of folder in which to store output" (remember quotes!)
##################################################################
#Copyright 2010 Bryan Killett
#
#This file is part of GAIA.
#
#GAIA is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#GAIA is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with GAIA. If not, see <http://www.gnu.org/licenses/>.
##################################################################

#Requires tail_gaia_log2.sh, internals_c2gaia.sh, builds internals_temp2.sh and creates multiple_cgaia_marker.txt so gaia creates copy_gaia_log2.sh, copy_idl_log2.sh and run2.pro. Also, overwrite scripts in GRACE replace the tail scripts with alternate versions for Azonic, Iliff.

file_list="definitions.hpp functions.cpp gaia.cpp c2gaia.sh internals_c2gaia.sh double_constants.hpp Makefile"

#Now compile the gaia.cpp program (which is make's default).
#But delete the binaries just in case errors prevent make from.. making.. a
#new one.  Also, rename the exec file so I know that it's a c2gaia instance.
rm -f ./gaia2.out
rm -f ./gaia.out
make
mv ./gaia.out ./gaia2.out

#Since gaia.out will make its own run.pro to handle IDL launching create_plots,
#it's a good idea to delete that file now so that if gaia.out fails, IDL won't
#be launched.
rm -f ../IDL/run2.pro
#Also, gaia.out creates a new copying script every time, so remove the old one.
rm -f ./copy_gaia_log2.sh
#Also, remove the old log files.
rm -f ./log_gaia2.txt
rm -f ./log_idl2.txt
#Also, remove temporary internal script.
rm -f ./internals_temp2.sh

#Remove multiple_cgaia_marker.txt, created by cXgaia (probably doesn't exist).
rm -f ./multiple_cgaia_marker.txt
#Create a new one, which will be deleted by gaia.cpp after it's used.
echo '2' > ./multiple_cgaia_marker.txt

#Also, remove source files zip file and create a new one.
rm -f ./c_source_files.zip
zip c_source_files $file_list

#Apparently I can't easily pass the runtime arg to internals_cgaia.sh.
echo '#!/bin/bash' > ./internals_temp2.sh
#c2gaia passes an argument to gaia which results in copy_gaia_log2.sh.
echo './gaia2.out "'"$1"'"' >> ./internals_temp2.sh
chmod +x ./internals_temp2.sh

#This other shell program is run with & so that all the commands in it are
#performed even if I log out of Azonic.
./internals_c2gaia.sh &

./tail_gaia_log2.sh
